package homework;

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.TreeSet;

public class HomeWork2 {
    public static void main(String[] args) {
        Set set = new HashSet();
        Goods goods1 = new Goods(1,"西游记",20.0,"人民出版社");
        Goods goods2 = new Goods(2,"水浒传",21.0,"作家出版社");
        Goods goods3 = new Goods(2,"水浒传",21.0,"作家出版社");
        set.add(goods1);
        set.add(goods2);
        set.add(goods3);
        System.out.println(set);

        Set set1 = new TreeSet();
        set1.add(goods1);
        set1.add(goods2);
        set1.add(goods3);
        System.out.println(set1);


    }
}
class Goods implements Comparable<Goods>{
    private int num;
    private String name;
    private double price;
    private String press;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Goods goods = (Goods) o;
        return num == goods.num && Double.compare(goods.price, price) == 0 && Objects.equals(name, goods.name) && Objects.equals(press, goods.press);
    }

    @Override
    public int hashCode() {
        return Objects.hash(num, name, price, press);
    }

    public Goods(int num, String name, double price, String press) {
        this.num = num;
        this.name = name;
        this.price = price;
        this.press = press;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getPress() {
        return press;
    }

    public void setPress(String press) {
        this.press = press;
    }

    @Override
    public String toString() {
        return "Goods{" +
                "num=" + num +
                ", name='" + name + '\'' +
                ", price=" + price +
                ", press='" + press + '\'' +
                '}';
    }

    @Override
    public int compareTo(Goods o) {
        return -name.compareTo(o.name);
    }
}